<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>延时交互队列</title>
  </head>
  <body>
    <button id="begin">开始</button>
    &nbsp;
    <button id="interaction">添加交互队列</button>

    <script>
      const begin = document.getElementById("begin");
      const interaction = document.getElementById("interaction");

      function delay(duration) {
        const start = Date.now();
        while (Date.now() - start < duration) {}
      }

      function addDelay() {
        console.log("添加延时队列");
        setTimeout(function () {
          console.log("延时队列执行");
        }, 100);
        delay(2000);
      }

      function addNetWork() {
        console.log("添加网络队列");
        fetch("./test.txt").then(resp => {
          console.log("网络队列执行");
        });
        delay(2000);
      }

      function addInteraction() {
        console.log("添加交互队列");
        interaction.onclick = function () {
          console.log("交互队列执行");
        };
        delay(2000);
      }

      begin.onclick = function () {
        addNetWork();
        addDelay();
        addInteraction();
        console.log("==========");
      };
    </script>
  </body>
</html>
